package server;

import java.net.*;
import java.io.*;
import java.util.*;

public class Server {
	  private Vector _tabClients = new Vector(); 
	  private int _nbClients=10;  //Number total of clients
	  private Vector<Person> database = new Vector();
	  private String path_database = "database.txt";

	  
	  // Main
	  public static void main(String args[]) {
		  Server server = new Server(); // Instance of main class 
		  try {
			  Integer port;
			  if(args.length<=0) {
				  port=new Integer("8888"); // By default using 8888 port number
			  } else {
				  port = new Integer(args[0]); // Input port using java input args
			  }

			  ServerSocket ss = new ServerSocket(port.intValue()); // Making a new socket on port
			  printWelcome(port); // just fun ^^*/
			  server.read_database(); // Reading database
			  
			  while (true) { // infinite loop => waiting connection
				  new Server_Thread(ss.accept(),server); // New client making thread
			  }
		  } catch (Exception e) {
			  System.out.println("[Error] : "+e);
		  }
	  }

	  
	 /**
	  * Credits and Fun
	  * @param port
	  */
	  static private void printWelcome(Integer port) {
		  System.out.println("-------------------------------------------------------------------");
		  System.out.println("Server Android Login : CHANG Wei & SHAN Zhibo");
		  System.out.println("Copyright : 2013 - MASTER 2 PSM");
		  System.out.println("-------------------------------------------------------------------");
		  System.out.println("Server listen on port : "+port.toString());
		  System.out.println("Server is OK");
		  System.out.println("-------------------------------------------------------------------");
		  System.out.println("Waiting for connection...");
	  }

	  
	  /**
	   * Deleting a client
	   * @param i
	   */
	  synchronized public void delClient(int i) {
		  _nbClients--; 
		  if (_tabClients.elementAt(i) != null) {
			  _tabClients.removeElementAt(i); 
		  }
	  }

	  
	  /**
	   * Add a client
	   * @param out
	   * @return
	   */
	  synchronized public int addClient(PrintWriter out) {
		  _nbClients++;
		  _tabClients.addElement(out); 
		  return _tabClients.size()-1; 
	  }

	  
	  /**
	   * Number of connected client
	   * @return
	   */
	  synchronized public int getNbClients() {
		  return _nbClients; 
	  }
	  
	  
	  /**
	   * Reading the database from a textfile
	   */
	  public void read_database() {
		  String ligne;
		  
		  try {
				InputStream ips = new FileInputStream(path_database); 
				InputStreamReader ipsr = new InputStreamReader(ips);
				BufferedReader br = new BufferedReader(ipsr);
				database.clear();
				while ((ligne=br.readLine())!=null) {
					String[] parameters = ligne.split(" ");
					System.out.println("User : "+parameters[0]+" - "+parameters[1]);
					database.add(new Person(parameters[0], parameters[1]));
				}
				br.close(); 
				System.out.println("Database readed...");
			} catch (Exception e){
				System.out.println("[ERROR] : "+e);
			}
	  }
	  
	  
	  /**
	   * Writing the database in a file
	   */
	  public void write_database() {
		  try {
			  FileWriter fw = new FileWriter(path_database, true);
			  BufferedWriter output = new BufferedWriter(fw);
			  
			  for(int i=0; i<database.size(); i++) {
				  output.write(database.get(i).getName()+" "+database.get(i).getPath_reference()+"\r\n");
				  output.flush();
			  }
			  
			  output.close();
			  System.out.println("Database saved...");
		  } catch(IOException e) {
			  System.out.print("[ERROR] : "+e);
		  }
	}


	public Vector<Person> getDatabase() {
		return database;
	}


	public void setDatabase(Vector<Person> database) {
		this.database = database;
	}
	  
	  
}
